package controllers;

import dto.BrandDto;
import models.Brand;
import models.crudsiena.SienaSupport;
import play.exceptions.TemplateNotFoundException;
import play.mvc.With;
import services.BrandService;

import java.util.List;

/**
 * Created by Gipxy group
 * Date: 5/9/12
 */
@With(Secure.class)
public class Brands extends CRUD{
    public static void list(int page, String filterField, String filterValue, String orderBy, String order) {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        if (page < 1) {
            page = 1;
        }

        List<? extends SienaSupport> objects = type.findPage(page, filterField, filterValue, orderBy, order);
        Long count = type.count(filterField, filterValue);
        Long totalCount = type.count(null, null);
        try {
            render(type, objects, count, totalCount, page, orderBy, order);
        } catch (TemplateNotFoundException e) {
            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
        }
    }

    public static void save(BrandDto dto) throws Exception {
        BrandService.merge(dto);
        redirect("Brands.list");
    }

    public static void input(Long id) {
        Brand model = (id == null) ? new Brand() : Brand.getByKey(Brand.class, id);
        BrandDto dto = BrandService.createDto(model);
        render(dto);
    }

    public static void show(String id) {
        redirect("Brands.input", id);
    }
}
